﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using SystemLYH.API.Application.Command.QualityCommand;
using SystemLYH.Domain;
using SystemLYH.Domain.Dto;

namespace SystemLYH.API.Controllers
{
    /// <summary>
    /// 质检API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class QualityController : ControllerBase
    {
        protected readonly IMediator _mediator;
        /// <summary>
        /// 中介者构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public QualityController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 过程质检添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateQuality(QualityCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 过程质检列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<Qualityduring>>> ShowQuality([FromQuery] QualityShowCommand command)
        {
            ApiResult<PageDTO<Qualityduring>> result = new ApiResult<PageDTO<Qualityduring>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 查看过程质检详情
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Qualityduring>> GetQuality([FromQuery] QualityBackfillCommand command)
        {
            ApiResult<Qualityduring> result = new ApiResult<Qualityduring>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 过程质检删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteQuality(QualityDelectCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 过程质检修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateQuality(QualityUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
